﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;

using Bpms.Log;
using Bpms.Config;

namespace Bpms.Control
{
    /// <summary>
    /// 页面基类
    /// </summary>
    public class BasePage : System.Web.UI.Page
    {

        protected internal LogAdapter log; //日志
        private bool isCheckLogin = true; //是否需要登录

        /// <summary>
        /// 标题 
        /// </summary>
        protected string PageTitle
        {
            get
            {
                return ConfigContext.GetPageTitle;
            }
        }
        /// <summary>
        ///设置是否需要登录
        /// </summary>
        public bool IsCheckLogin
        {
            set
            {
                isCheckLogin = value;
            }
            get
            {
                return this.isCheckLogin;
            }
        }

        /// <summary>
        /// 页面编码
        /// </summary>
        /// <param name="data"></param>
        /// <returns>编码后的字符串</returns>
        public string HtmlEncode(object data)
        {
            return Bpms.Common.Security.HtmlEncode(data.ToString());
        }

        /// <summary>
        /// 页面加载
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            Bpms.Common.Security.CheckRequestValidity(Request);

            if (IsCheckLogin)
            {
                //    //CheckLogin();
            }

            AddStylesheetIncludeAt(0, ResolveUrl("~/css/jquery/jquery.alert.css"));
            AddJavaScriptIncludeAt(1, ResolveUrl("~/js/jquery/jquery.js"));
            //   AddJavaScriptInclude(ResolveUrl("~/js/jquery/ui/ui.core.js"));
            AddJavaScriptIncludeAt(2, ResolveUrl("~/js/jquery/jquery.alert.js"));
        }

        /// <summary>
        /// 捕获错误
        /// </summary>
        /// <param name="e"></param>
        protected override void OnError(EventArgs e)
        {

            //Exception exception = Server.GetLastError();

            //if (exception != null)
            //{
            //    Server.ClearError();
            //    //写入异常日志
            //    log = new ExceptionLogAdapter();
            //    log.WriteLog(Request.Url.ToString(), exception);
            //}

            base.OnError(e);
        }

        /// <summary>
        /// 向页面添加 Meta Content
        /// </summary>
        protected virtual void AddMetaContentType()
        {
            HtmlMeta meta = new HtmlMeta();
            meta.HttpEquiv = "content-type";
            meta.Content = Response.ContentType + "; charset=" + Response.ContentEncoding.HeaderName;
            Page.Header.Controls.Add(meta);
        }

        /// <summary>
        /// 向页面添加 Meta tag
        /// </summary>
        protected virtual void AddMetaTag(string name, string value)
        {
            if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(value))
                return;

            HtmlMeta meta = new HtmlMeta();
            meta.Name = name;
            meta.Content = value;
            Page.Header.Controls.Add(meta);
        }

        /// <summary>
        /// 向页面添加 JS 文件
        /// </summary>
        /// <param name="url"></param>
        protected virtual void AddJavaScriptInclude(string url)
        {
            HtmlGenericControl script = new HtmlGenericControl("script");
            script.Attributes["type"] = "text/javascript";
            script.Attributes["language"] = "javascript";
            script.Attributes["src"] = url;
            Page.Header.Controls.Add(script);
        }

        protected virtual void AddJavaScriptIncludeAt(int index, string url)
        {
            HtmlGenericControl script = new HtmlGenericControl("script");
            script.Attributes["type"] = "text/javascript";
            script.Attributes["language"] = "javascript";
            script.Attributes["src"] = url;
            Page.Header.Controls.AddAt(index, script);
        }

        /// <summary>
        ///  向页面添加 CSS 文件
        /// </summary>
        /// <param name="url"></param>
        protected virtual void AddStylesheetInclude(string url)
        {
            HtmlLink link = new HtmlLink();
            link.Attributes["type"] = "text/css";
            link.Attributes["href"] = url;
            link.Attributes["rel"] = "stylesheet";
            Page.Header.Controls.Add(link);
        }

        protected virtual void AddStylesheetIncludeAt(int index, string url)
        {
            HtmlLink link = new HtmlLink();
            link.Attributes["type"] = "text/css";
            link.Attributes["href"] = url;
            link.Attributes["rel"] = "stylesheet";
            Page.Header.Controls.AddAt(index, link);
        }

        protected virtual void Alert(string message)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>  jAlert('" + message + "','bpms'); </script>");
        }

        protected virtual void AlertBack(string message)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>  jAlert('" + message + "','bpms',function(result){if(result){history.go(-1);}}); </script>");
        }

        protected virtual void AlertRedirect(string message, string url)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>  jAlert('" + message + "','bpms',function(result){if(result){ location.href='" + url + "';}}) </script>");
        }

        protected virtual void Confirm(string message)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "", "<script>  jConfirm('" + message + "'); </script>");
        }
    }
}
